package com.iwinding.ladygg.ui.act;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import com.iwinding.ladygg.AppForegroundStateManager;
import com.iwinding.ladygg.ICaneApplication;
import com.iwinding.ladygg.R;
import com.iwinding.ladygg.bluetooth.impblekit.ICaneBleApi;
import com.iwinding.ladygg.utils.WindowUtil;

/**
 * Created by Administrator on 2016/6/17.
 */
public abstract class FragmentBaseActivity extends AppCompatActivity {

    protected ICaneApplication mOAAplication;
    protected ImageView mBack;
    protected TextView mTitle;
    protected ImageView mRight;
    protected TextView mRightTxt;
    protected LinearLayout mContent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        WindowUtil.bepaintStatusBar(this, getResources().getColor(R.color.colorPink));

        mOAAplication = (ICaneApplication) getApplication();

        setContentView(R.layout.icane_activity_base);

        mBack = (ImageView) findViewById(R.id.base_head_back);
        mTitle = (TextView) findViewById(R.id.base_head_title);
        mRight = (ImageView) findViewById(R.id.base_head_right);
        mRightTxt = (TextView) findViewById(R.id.base_right_txt);
        mContent = (LinearLayout) findViewById(R.id.base_content);

        mContent.addView(getContent());

        if (mBack != null) {
            mBack.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        }
    }

    protected View getContent() {
        return View.inflate(this, getLayoutID(), null);
    }

    //加载布局
    protected abstract int getLayoutID();

    //需要替换的布局
    protected abstract int getFragmentID();

    //管理fragments
    protected abstract void handleFragments(FragmentTransaction transaction, Fragment currentFragment, List<Fragment> list);


//    protected void setBackFunction(){
//
//    }

    protected void setTitleStr(String title) {
        if (mTitle != null) {
            mTitle.setText(title);
        }
    }

    protected void setTitleResId(int resString) {
        if (mTitle != null) {
            mTitle.setText(getString(resString));
        }
    }

    protected void setmRightIcon(int resIcon, View.OnClickListener onClickListener) {
        if (mRight != null) {
            mRight.setImageResource(resIcon);
            mRight.setVisibility(View.VISIBLE);
            mRight.setOnClickListener(onClickListener);
        }
    }
    protected void setmRightTxtStr(String str, View.OnClickListener onClickListener) {
        if (mRightTxt != null) {
            mRightTxt.setText(str);
            mRightTxt.setVisibility(View.VISIBLE);
            mRightTxt.setOnClickListener(onClickListener);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        ICaneBleApi.getInstance().initContext(this);
        if (mRight != null)
            mRight.setVisibility(mBack.getVisibility() != View.GONE   ? View.INVISIBLE : View.GONE);
        AppForegroundStateManager.getInstance().onActivityVisible(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        AppForegroundStateManager.getInstance().onActivityNotVisible(this);
    }


    public void updateFragment(Fragment fragment) {
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        List<Fragment> list = manager.getFragments();
        handleFragments(transaction, fragment, list);
        if (fragment.isAdded()) {
            transaction.show(fragment);
        } else {
            transaction.add(getFragmentID(), fragment);
        }
        transaction.commitAllowingStateLoss();
    }

    public void clearFragments() {
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        List<Fragment> list = manager.getFragments();
        if (list == null)
            return;
        for (Fragment frag : list) {
            if (frag != null && frag.isAdded()) {
                transaction.remove(frag);
                transaction.commitAllowingStateLoss();
            }
        }
    }

}
